package com.banmao.youZone.config.web.resolver;

import com.alibaba.fastjson2.JSONObject;
import com.banmao.youZone.config.web.annotation.RequestSingleParam;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;
import org.springframework.web.util.ContentCachingRequestWrapper;
import org.springframework.web.util.WebUtils;

import java.io.BufferedReader;

/**
 * RequestStringParam参数解析器 实现 HandlerMethodArgumentResolver 接口
 *
 * @author created by wutong li on 2021/04/29
 */
public class RequestStringParamHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver {

    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        return parameter.hasParameterAnnotation(RequestSingleParam.class);
    }

    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
                                  NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        RequestSingleParam requestSingleParam = parameter.getParameterAnnotation(RequestSingleParam.class);

        HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);

        StringBuilder sb = new StringBuilder();
        ContentCachingRequestWrapper wrapper = WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class);
        if (wrapper.getContentAsByteArray() == null || wrapper.getContentAsByteArray().length == 0) {
            BufferedReader reader = request.getReader();
            char[] buf = new char[1024];
            int rd;
            while ((rd = reader.read(buf)) != -1) {
                sb.append(buf, 0, rd);
            }
        } else {
            sb.append(new String(wrapper.getContentAsByteArray()));
        }

        JSONObject jsonObject = JSONObject.parseObject(sb.toString());
        String value = requestSingleParam.value();
        return jsonObject.get(value);
    }

}
